<!DOCTYPE html>
<html>
<head>
    <title>文章管理</title>
    <#include "/header.html">
    <style>
        .layui-table img {
            max-height: 30px;
        }
    </style>
</head>
<body>
<div class="layuimini-container">
    <div class="layuimini-main">
        <div style="margin: 10px 0">
            <form class="layui-form layui-form-pane" action="" id="searchForm">
                <div class="layui-form-item">
                    <div class="layui-inline">
                        <label class="layui-form-label">标题</label>
                        <div class="layui-input-inline">
                            <input type="text" name="title" placeholder="搜索标题" autocomplete="off" class="layui-input">
                        </div>
                    </div>
                    <div class="layui-inline">
                        <button type="button" class="layui-btn layui-btn-primary layui-border-blue" lay-submit lay-filter="form-search-btn"><i class="layui-icon layui-icon-search"></i>搜索</button>
                        <button type="button" class="layui-btn layui-btn-primary" lay-submit lay-filter="form-reset-btn"><i class="layui-icon layui-icon-refresh"></i>重置</button>
                    </div>
                </div>
            </form>
        </div>

        <table class="layui-table" id="currentTableId"></table>
    </div>
</div>

<script type="text/html" id="toolbarDemo">
    <div>
        <#if shiro.hasPermission("app:article:save")>
        <button class="layui-btn layui-btn-normal" lay-event="add"><i class="layui-icon layui-icon-add-circle"></i>添加文章</button>
        </#if>
    </div>
</script>

<script type="text/html" id="currentTableTypeTpl">
    {{# if(d.type == 1){ }}
    <span class="layui-badge-rim layui-border-black">目录</span>
    {{# } else { }}
    <span class="layui-badge-rim layui-border-orange">文章</span>
    {{# } }}
</script>

<script type="text/html" id="currentTableStatusTpl">
    {{# if(d.status == 1){ }}
    <span class="layui-badge layui-bg-green">启用</span>
    {{# } else { }}
    <span class="layui-badge layui-bg-grey">禁用</span>
    {{# } }}
</script>

<script type="text/html" id="imgtemp">
	<div><a href="{{d.img}}" target="_blank"><img src="{{d.img}}" class="layui-nav-img" style="width:20px;height:20px"></a></div>
</script>
<script type="text/html" id="currentTableBar">
    <#if shiro.hasPermission("app:article:list")>
    <a class="layui-btn layui-btn-primary layui-border-blue layui-btn-xs" lay-event="edit"><i class="layui-icon layui-icon-edit"></i>编辑</a>
    </#if>
    <#if shiro.hasPermission("app:article:list")>
    <a class="layui-btn layui-btn-primary layui-border-red layui-btn-xs" lay-event="delete"><i class="layui-icon layui-icon-delete"></i>删除</a>
    </#if>
</script>

<script type="text/javascript">
    var formDataId = 0;
    var menuTreeData = [];
    var table;

    function loadMenuData() {
        menuTreeData = [];
        $.ajax({
            type: "POST",
            url: serverUrl('article/tree'),
            data: {},
            dataType: "json",
            success: function (result) {
                menuTreeData = result;
            }
        });
    }

    $(document).ready(function () {
        loadMenuData();
    });

    layui.use(['form', 'treeTable'], function () {
        var $ = layui.jquery,
            form = layui.form,
            treeTable = layui.treeTable;

        layer.load(2);
        // 渲染树形表格
        table=tableSearch({},'article/list');

        treeTable.on('toolbar(currentTableId)', function (obj) {
            if (obj.event === 'add') {  // 监听添加操作
                formDataId = 0;
                openFormView('app/article/form.html', '添加文章');
            }
        });

        treeTable.on('tool(currentTableId)', function (obj) {
            var data = obj.data;
            if (obj.event === 'edit') {
                formDataId = data.id;
                openFormView('app/article/form.html', '编辑文章');
                return false;
            } else if (obj.event === 'delete') {
                layer.confirm('确定删除 '+data.title+' 吗？', function (index) {
                    deleteTableRow('article/delete/', [data.id], function (rs) {
                        table.reload();
                    });
                    layer.close(index);
                });
            }
        });
        function tableSearch(data,url) {
        	var tablethis = treeTable.render({
                elem: '#currentTableId',
			    where:data,
                url: serverUrl(url),
                tree: {
                    iconIndex: 2,           // 折叠图标显示在第几列
                    isPidData: true,        // 是否是id、pid形式数据
                    idName: 'id',  // id字段名称
                    pidName: 'parentId'     // pid字段名称
                },
                toolbar: '#toolbarDemo',
                cols: [[
                    {type: 'numbers', width: 80},
                    {field: 'icon', title: '图标', minWidth: 70, width: 70, templet: '#imgtemp'},
                    {field: 'title', minWidth: 150, title: '标题'},
                    {field: 'content', minWidth: 150, title: '内容'},
                    {field: 'oknum', title: '点赞', minWidth: 60},
                    {field: 'looknum', title: '浏览', minWidth: 60},
                    {field: 'createtime', minWidth: 120, title: '时间'},
                    {field: 'remark', minWidth: 100, title: '备注'},
                    /* {field: 'type', title: '类型', width: 80, templet: '#currentTableTypeTpl'},
                    {field: 'status', title: '状态', width: 80, templet: '#currentTableStatusTpl'},
                    {field: 'sort', width: 80, align: 'center', title: '排序'}, */
                    {templet: '#currentTableBar', width: 150, align: 'center', title: '操作'}
                ]],
                done: function () {
                    layer.closeAll('loading');
                }
            });
        	return tablethis;
        };
        // 监听搜索操作
        form.on('submit(form-search-btn)', function (data) {
            //执行搜索重载
            if (data.field.title) {
            	table=tableSearch({"title":data.field.title},'article/search');
            //table.filterData(data.field.title);
	        } else {
	            table.clearFilter();
	        }
	        return false;
	    });
        form.on('submit(form-reset-btn)', function (data) {
            table=tableSearch({},'article/list');
            $("#searchForm")[0].reset();
            table.clearFilter();
            return false;
        });

    });
</script>

</body>
</html>